/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package failuremodule;

import comunicacao.GestorZooKeeper;
import entities.Server;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class GestorPainel {

    private Painel painel;
    private GestorZooKeeper gestorZK;
    private ArrayList<Server> servers;

    public GestorPainel(Painel painel) {
        this.painel = painel;

        gestorZK = new GestorZooKeeper();
    }

    public void start() {
        servers = gestorZK.getAllServers();
        painel.setLayout(new BorderLayout());

        if (servers != null && servers.size() > 0) {
            addServerPainel();
        } else {
            JLabel txt = new JLabel("não há servidores");
            painel.add(txt);

        }

    }

    private void addServerPainel() {

        JPanel totalGUI = new JPanel();
        JPanel mainPanel = new JPanel(new GridLayout(3, 4, 10, 30));
        for (Server server : servers) {
            ServerPainel sp = new ServerPainel();
            sp.setServer(server);
            sp.nodeNameLabel.setText(server.getNodeName());
            mainPanel.add(sp);

        }

        totalGUI.add(mainPanel);
        totalGUI.setOpaque(true);
        painel.add(totalGUI);
    }

    public void example() {
        GridLayoutExample gl = new GridLayoutExample();

        painel.setLayout(new BorderLayout());

        //painel.add(gl.createContentPane());
        painel.add(test());

    }

    private JPanel test() {

        JPanel totalGUI = new JPanel();

        // We create a JPanel with the GridLayout.
        JPanel mainPanel = new JPanel(new GridLayout(3, 4, 10, 30));
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());
        mainPanel.add(new ServerPainel());

        totalGUI.add(mainPanel);
        totalGUI.setOpaque(true);
        return totalGUI;

    }

}
